package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.HisDepts;
import com.ruoyi.system.service.IHisDeptsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author Admin
 * @title: HisDeptController
 * @projectName RuoYi-Vue-master
 * @description: HisDeptController
 * @date 2025/4/13 19:00
 */
@RestController
@RequestMapping("/his/depts")
public class HisDeptController extends BaseController {
    @Autowired
    private IHisDeptsService hisDeptsService;

    /**
     * 获取科室列表
     */
    @PreAuthorize("@ss.hasPermi('his:depts:list')")
    @GetMapping("/list")
    public TableDataInfo list(HisDepts hisDepts) {
        startPage();
        List<HisDepts> list = hisDeptsService.selectDeptsList(hisDepts);
        return getDataTable(list);
    }

    /**
     * 科室删除
     */
    //@PreAuthorize("@ss.hasPermi('system:post:remove')")
    //@Log(title = "科室管理", businessType = BusinessType.DELETE)
    //@DeleteMapping("/{deptsId}")
    //public AjaxResult remove(@PathVariable Long deptsId)
    //{
    //    return toAjax(hisDeptsService.deleteDeptsById(deptsId));
    //}

    @PreAuthorize("@ss.hasPermi('his:depts:add')")
    @Log(title = "科室新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody HisDepts hisDepts) {
        //if (!postService.checkPostNameUnique(post))
        //{
        //    return error("新增岗位'" + post.getPostName() + "'失败，岗位名称已存在");
        //}
        //else if (!postService.checkPostCodeUnique(post))
        //{
        //    return error("新增岗位'" + post.getPostName() + "'失败，岗位编码已存在");
        //}
        //添加创建人名称
        hisDepts.setCreateBy(getUsername());
        hisDepts.setCreateTime(new Date());
        return toAjax(hisDeptsService.insertHisDepts(hisDepts));
    }

    /**
     * 根据科室id回显信息
     *
     * @param deptsId
     * @return
     */
    @PreAuthorize("@ss.hasPermi('his:depts:query')")
    @GetMapping(value = "/{deptsId}")
    public AjaxResult getInfo(@PathVariable Long deptsId) {
        return success(hisDeptsService.selectHisDeptsById(deptsId));
    }

    /**
     * 修改科室
     */
    @PreAuthorize("@ss.hasPermi('his:depts:edit')")
    @Log(title = "科室管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody HisDepts hisDepts) {
        //if (!postService.checkPostNameUnique(post))
        //{
        //    return error("修改岗位'" + post.getPostName() + "'失败，岗位名称已存在");
        //}
        //else if (!postService.checkPostCodeUnique(post))
        //{
        //    return error("修改岗位'" + post.getPostName() + "'失败，岗位编码已存在");
        //}
        hisDepts.setUpdateBy(getUsername());
        hisDepts.setUpdateTime(new Date());
        return toAjax(hisDeptsService.updateHisDepts(hisDepts));
    }

    /**
     * 批量删除科室
     */
    @PreAuthorize("@ss.hasPermi('his:depts:remove')")
    @Log(title = "科室管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{deptsIds}")
    public AjaxResult remove(@PathVariable Long[] deptsIds)
    {
        return toAjax(hisDeptsService.deleteDeptsByIds(deptsIds));
    }
}
